package oceanlife;

import java.util.ArrayList;
import java.util.Collections;

public class AgentVisibilityLine {
	private ArrayList<Double> startRadian = new ArrayList<Double>();
	private ArrayList<Double> stopRadian = new ArrayList<Double>();
	
	
	public boolean addAgent(Double start, Double stop){
		ArrayList<Integer> removeElements = new ArrayList<Integer>();
		for(int i = 0; i < startRadian.size();i++){
			//following is true if no part of agent is visible
			if(start >= startRadian.get(i) && stop <= stopRadian.get(i)){
				return false;
			}
			if(start <= startRadian.get(i) && stop >= stopRadian.get(i)){
				removeElements.add(i);
			} else 	if((start >= startRadian.get(i) && start <= stopRadian.get(i))
					|| (stop >= startRadian.get(i) && stop <= stopRadian.get(i))){
				start = Math.min(start, startRadian.get(i));
				stop = Math.max(stop, stopRadian.get(i));
				removeElements.add(i);
			}
		}
		for(int i = removeElements.size()-1 ; i >= 0; i--){
			//System.out.println("removing: "+startRadian.get(removeElements.get(i))+" "+stopRadian.get(removeElements.get(i)));
			startRadian.remove((int)removeElements.get(i));
			stopRadian.remove((int)removeElements.get(i));
		}
		startRadian.add(start);
		stopRadian.add(stop);
		return true;
	}
	
	public void print(){
		System.out.println("items: "+startRadian.size()+"/"+stopRadian.size());
		for(int i = 0; i < startRadian.size();i++){
			System.out.println(startRadian.get(i) + " " + stopRadian.get(i));
		}
	}
	
	public static void main (String[] args){
		AgentVisibilityLine avl = new AgentVisibilityLine();
		avl.addAgent(0.0,1.0);
		avl.addAgent(1.1,1.2);
		avl.addAgent(1.5,2.5);
		avl.addAgent(3.5,3.6);
		avl.addAgent(0.5,2.0);
		avl.print();
	}
}
